package com.hxzy.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxzy.common.constans.RedisConstans;
import com.hxzy.entity.SysDictData;
import com.hxzy.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Desc 字典缓存工具类
 * @Author: admin
 * @Date: 2023/07/03/14:16
 */
@Component
public class DictCacheUtil {

    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * springbean生命周期回调函数
     */
    @PostConstruct
    public void lifeCircleInit(){
        this.removeAllRedisDictData();
        this.loadAllDictDataToRedis();
    }



    /**
     * 加载sys_dict_data所有的数据到redis缓存中
     */
    public void loadAllDictDataToRedis(){
        List<SysDictData> dictDataList = this.sysDictDataService.searchAllData();
        Map<String, List<SysDictData>> collect = dictDataList.stream().collect(Collectors.groupingBy(SysDictData::getDictType));
        for(String key : collect.keySet()){
            String redisKey= RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY, key);
            this.redisTemplate.opsForValue().set( redisKey, collect.get(key));
        }
    }

    /**
     * 移出redis中所有的sys_dict_data缓存
     */
    public void removeAllRedisDictData(){
        String redisKey= RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY, "*");
        Set keys = this.redisTemplate.keys(redisKey);
        this.redisTemplate.delete(keys);
    }


    /**
     * 根据dictType查询缓存
     * @param dictType
     * @return
     */
    public List<SysDictData>  findByDictType(String dictType){
        String redisKey= RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY, dictType);
        Object redisValue = this.redisTemplate.opsForValue().get(redisKey);
        if(Objects.isNull(redisValue)){
            return null;
        }
        return (List<SysDictData>) redisValue;
    }

    /**
     * 刷新redis的某个类型的缓存数据
     * @param dictType
     * @return
     */
    public void refreshCacheByDictType(String dictType){
        String redisKey=RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,dictType);

        LambdaQueryWrapper<SysDictData> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictType,dictType);
        queryWrapper.eq(SysDictData::getStatus,"0");

        List<SysDictData> list = this.sysDictDataService.list(queryWrapper);
        //没有数据，就要移出缓存
        if(list.size()==0){
            this.redisTemplate.delete(redisKey);
        }else{
            //新增缓存
            this.redisTemplate.opsForValue().set(redisKey,list);
        }
    }

    /**
     * 从缓存中移出某个指定的键
     * @param dictType
     */
    public void removeByDictType(String dictType){
        String redisKey=RedisConstans.getRedisKey(RedisConstans.DICT_DATA_KEY,dictType);
        this.redisTemplate.delete(redisKey);
    }

}
